//
//  DateTools.m
//  CommonTools
//
//  Created by xing dx on 13-1-6.
//  Copyright (c) 2013年 guowk. All rights reserved.
//

#import "DateTools.h"

/** 一天的时间的毫秒数 */
#define Day_second 86400

static DateTools *pTools = nil;
@implementation DateTools

#pragma mark -
#pragma mark ==========================================
#pragma mark init 初始化等设置
+ (id)getInstance {
  @synchronized(self) {
		if (pTools == nil) {
			pTools = [[DateTools alloc] init];
		}
	}
  
  return pTools;
}

#pragma mark -
#pragma mark ==========================================
#pragma mark 输出error

- (void)printError:(NSString *)error {
  printf("\n* ----------------------------");
  printf("\n* error: %s",[error UTF8String]);
  printf("\n* ----------------------------");
}

#pragma mark -
#pragma mark ==========================================
#pragma mark 日期格式转换
- (NSString *)dateToString:(NSDate *)date separator:(NSString *)sep {
  NSString *result = @"";
  
  if (date == nil && sep == nil) {
    [self printError:@"输入参数错误！"];
  }
  
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  dateFormatter.dateFormat = sep;
  result = [dateFormatter stringFromDate:date];
  
  return result;
}

- (NSDate *)stringToDate:(NSString *)string separator:(NSString *)sep {
  NSDate *result = [NSDate date];
  
  if (string == nil && sep == nil) {
    [self printError:@"输入参数错误！"];
  }
  
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  dateFormatter.dateFormat = sep;
  result = [dateFormatter dateFromString:string];
  
  return result;
}

- (NSString *)stringToString:(NSString *)fromString
               fromSeparator:(NSString *)fromSep
                    toString:(NSString *)toString
                 toSeparator:(NSString *)toSep {
  NSString *result = fromString;
  
  if (fromString == nil && fromSep == nil && toString == nil && toSep == nil) {
    [self printError:@"输入参数错误！"];
  }
  
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  dateFormatter.dateFormat = fromSep;
  NSDate *date = [dateFormatter dateFromString:fromString];
  
  dateFormatter.dateFormat = toSep;
  result = [dateFormatter stringFromDate:date];
  
  return result;
}

#pragma mark -
#pragma mark ==========================================
#pragma mark 比较日期

- (BOOL)isLeapYear:(int)year {
  BOOL result = NO;
  if ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))) {
    result = YES;
  }
  return result;
}

- (BOOL)compareBOOL:(NSDate *)firstDate secondDate:(NSDate *)secondDate {
  BOOL result = NO;
  
  //判断参数是否合法
  if (firstDate == nil && secondDate == nil) {
    [self printError:@"输入参数错误！"];
  }
  
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  dateFormatter.dateFormat = @"yyyyMMdd";
  double first = [[dateFormatter stringFromDate:firstDate] doubleValue];
  double second = [[dateFormatter stringFromDate:secondDate] doubleValue];
  if (first >= second) {
    result = YES;
  }
  
  return result;
}

- (int)compareNumber:(NSDate *)firstDate secondDate:(NSDate *)secondDate {
  int result = 0;
  
  if (firstDate == nil && secondDate == nil) {
    [self printError:@"输入参数错误！"];
  }
  
  BOOL flag = [self compareBOOL:firstDate secondDate:secondDate];
  NSTimeInterval dayNumber = [firstDate timeIntervalSince1970];
  
  BOOL isTrue = YES;
  while (isTrue) {
    if (flag) {  /// firstDate 大于等于 secondDate
      int number = dayNumber + result * Day_second;
      NSDate *date = [NSDate dateWithTimeIntervalSince1970:number];
      BOOL b = [self compareBOOL:date secondDate:secondDate];
      if (!b) {
        isTrue = NO;
        result++;
      }
      else {
        result--;
      }
      
    }
    else { /// firstDate 小于 secondDate
      int number = dayNumber + result * Day_second;
      NSDate *date = [NSDate dateWithTimeIntervalSince1970:number];
      BOOL b = [self compareBOOL:date secondDate:secondDate];
      if (b) {
        isTrue = NO;
      }
      else {
        result++;
      }
      
    }
  }
  
  return result;
}

- (int)age:(NSDate *)birthday {
  int age = 0;
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy:MM:dd"];
	NSArray *mArray = [[dateFormatter stringFromDate:birthday] componentsSeparatedByString:@":"];
	int o_year = [[mArray objectAtIndex:0] intValue];
	// int o_month = [[mArray objectAtIndex:1] intValue];
  
  mArray = [[dateFormatter stringFromDate:[NSDate date]] componentsSeparatedByString:@":"];
  int n_year = [[mArray objectAtIndex:0] intValue];
	// int n_month = [[mArray objectAtIndex:1] intValue];
  if (n_year > o_year) {
    age = n_year - o_year;
  }
  return age;
}

- (int)weekForDate:(NSDate *)date {
  int result = 1;
  
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  dateFormatter.dateFormat = @"EEE";
  NSString *a_E_str = [dateFormatter stringFromDate:date];
  
  if ([a_E_str isEqualToString:@"Mon"]) {
    result = 1;
  }
  else if ([a_E_str isEqualToString:@"Tue"]) {
    result = 2;
  }
  else if ([a_E_str isEqualToString:@"Wed"]) {
    result = 3;
  }
  else if ([a_E_str isEqualToString:@"Thu"]) {
    result = 4;
  }
  else if ([a_E_str isEqualToString:@"Fri"]) {
    result = 5;
  }
  else if ([a_E_str isEqualToString:@"Sat"]) {
    result = 6;
  }
  else if ([a_E_str isEqualToString:@"Sun"]) {
    result = 7;
  }
  
  return result;
}

#pragma mark -
#pragma mark ==========================================
#pragma mark 根据条件获得日期

- (NSDate *)dateFromDate:(NSDate *)date dayCount:(int)number {
  NSDate *resultDate = date;
  
  NSTimeInterval dayNumber = [date timeIntervalSince1970];
  dayNumber += number * Day_second;
  resultDate = [NSDate dateWithTimeIntervalSince1970:dayNumber];
  
  return resultDate;
}

- (NSArray *)dateArrayFromDate:(NSDate *)date dayCount:(int)number {
  NSMutableArray *result = [NSMutableArray arrayWithCapacity:number];
  
  NSTimeInterval dayNumber = [date timeIntervalSince1970];
  
  if (number < 0) {
    number = abs(number);
    for (int i = 0; i < number; i++) {
      dayNumber -= Day_second;
      [result addObject:[NSDate dateWithTimeIntervalSince1970:dayNumber]];
    }
  }
  else {
    for (int i = 0; i < number; i++) {
      dayNumber += Day_second;
      [result addObject:[NSDate dateWithTimeIntervalSince1970:dayNumber]];
    }
  }
  
  return result;
}

- (NSArray *)dateArrayFromDate:(NSDate *)date dayCount:(int)number separator:(NSString *)sep {
  NSMutableArray *result = [NSMutableArray arrayWithCapacity:number];
  
  NSTimeInterval dayNumber = [date timeIntervalSince1970];
  
  if (number < 0) {
    number = abs(number);
    for (int i = 0; i < number; i++) {
      dayNumber -= Day_second;
      [result addObject:[self dateToString:[NSDate dateWithTimeIntervalSince1970:dayNumber] separator:sep]];
    }
  }
  else {
    for (int i = 0; i < number; i++) {
      dayNumber += Day_second;
      [result addObject:[self dateToString:[NSDate dateWithTimeIntervalSince1970:dayNumber] separator:sep]];
    }
  }

  return result;
}

- (NSDate *)preDate {
  return [self dateFromDate:[NSDate date] dayCount:-1];
}

- (NSDate *)nextDate {
  return [self dateFromDate:[NSDate date] dayCount:1];
}

@end
